package org.itboys.weixin.controller;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.weixin.entity.WeixinMessage;
import org.itboys.weixin.entity.WeixinMessageKeyword;
import org.itboys.weixin.service.WeixinMessageKeywordService;
import org.itboys.weixin.service.WeixinMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/weixin/keyword")
public class WeixinMessageKeywordController extends BaseController {

	@Autowired
	private WeixinMessageKeywordService weixinMessageKeywordService;
	@Autowired
	private WeixinMessageService weixinMessageService;
	@Autowired
	private ResourceHolder resourceHolder;
	
	@RequestMapping("/vm/{weixinId}")
	public ModelAndView projectMenus(@PathVariable("weixinId") Long weixinId,HttpServletRequest request,Model model){
		model.addAttribute("weixinId",weixinId);
		return new ModelAndView("/weixin/keyword/list");
	}
	
	@RequestMapping("/list")
	public void list(HttpServletResponse response, HttpServletRequest request){
		final Map<String,Object> sqlMap = QueryParamUtils.builderQueryMap(request);
		List<WeixinMessageKeyword> list = weixinMessageKeywordService.list(sqlMap);
		JsonPageUtils.renderJsonPage(list!=null&&list.size()>0?list.size():0L, list, response);
	}
	
	
	@RequestMapping("/getById")
	public ModelAndView getById(HttpServletRequest request,HttpServletResponse response,Model model){
		String id = request.getParameter("id");
		String weixinId = request.getParameter("weixinId");
		model.addAttribute("weixinId", weixinId);
		Map<String,Object> sqlMap=Maps.newHashMap();
		sqlMap.put("weixinId", weixinId);
		List<WeixinMessage> list=weixinMessageService.list(sqlMap);
		model.addAttribute("list", list);
		if(StringUtils.isNotBlank(id)){
			WeixinMessageKeyword entity = weixinMessageKeywordService.getById(Long.valueOf(id));
			model.addAttribute("entity", entity);
		}
		/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
		return  new ModelAndView("/weixin/keyword/input");
	}
	
	@RequestMapping("/save")
	public void insert(HttpServletResponse response, @ModelAttribute WeixinMessageKeyword weixinMessageKeyword){
		try {
			if(weixinMessageKeyword.getId()==0L){
				weixinMessageKeywordService.save(weixinMessageKeyword);
			}else{
				weixinMessageKeywordService.update(weixinMessageKeyword);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("weixinmessagekeyword insert error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	@RequestMapping("/delete")
	public void delete(HttpServletResponse response,HttpServletRequest request){
		try{
			Long id =  Long.valueOf(request.getParameter("id"));
			AjaxUtils.renderText(response, String.valueOf(weixinMessageKeywordService.delete(id)));
		}catch (Exception e) {
			logger.error("weixinmessagekeyword delete error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}
		
	@RequestMapping("/checkkeyword")
	public void checkkeyword(HttpServletResponse response,HttpServletRequest request){
		try{
			String id=request.getParameter("id");
			String weixinId=request.getParameter("weixinId");
			String keyword=request.getParameter("keyword");
			Map<String,Object> map=Maps.newHashMap();
			map.put("weixinId", weixinId);
			map.put("keyword", keyword);
			List<WeixinMessageKeyword> list = weixinMessageKeywordService.list(map);
			if(list!=null&&(list.size()>1||(list.size()==1&&StringUtils.isNotBlank(id)&&list.get(0).getId()!=Long.valueOf(id)))){
				AjaxUtils.renderJson(response,-1);//"已存在"
			}
			AjaxUtils.renderJson(response,1);
		}catch (Exception e) {
			logger.error("weixinmessagekeyword checkkeyword error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}
	
}
